package com.gymmsystem.GYMMSystem.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gymmsystem.GYMMSystem.common.Result;
import com.gymmsystem.GYMMSystem.entity.MemberCardRecord;
import com.gymmsystem.GYMMSystem.service.MemberCardRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/member-card-records")
public class MemberCardRecordController {

    @Autowired
    private MemberCardRecordService memberCardRecordService;

    /**
     * 获取用户的会员卡购买记录
     */
    @GetMapping("/user/{userId}")
    public Result getRecordsByUser(@PathVariable Long userId) {
        try {
            List<MemberCardRecord> records = memberCardRecordService.getRecordsByUserId(userId);
            return Result.success(records, "获取会员卡记录成功");
        } catch (Exception e) {
            log.error("获取会员卡记录失败 - 用户ID: {}, 错误: {}", userId, e.getMessage());
            return Result.error("获取会员卡记录失败");
        }
    }

    /**
     * 分页查询所有会员卡购买记录
     */
    @GetMapping("/page")
    public Result getRecordsPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            IPage<MemberCardRecord> page = memberCardRecordService.getRecordsPage(pageNum, pageSize);
            return Result.success(page, "获取会员卡记录成功");
        } catch (Exception e) {
            log.error("分页查询会员卡记录失败 - 页码: {}, 错误: {}", pageNum, e.getMessage());
            return Result.error("获取会员卡记录失败");
        }
    }

    /**
     * 购买会员卡
     */
    @PostMapping("/purchase")
    public Result purchaseCard(@RequestBody MemberCardRecord record) {
        try {
            boolean success = memberCardRecordService.purchaseCard(record);
            if (success) {
                return Result.success(null, "会员卡购买成功");
            } else {
                return Result.error("会员卡购买失败");
            }
        } catch (Exception e) {
            log.error("会员卡购买异常 - 用户ID: {}, 错误: {}", record.getUserId(), e.getMessage());
            return Result.error("会员卡购买异常");
        }
    }

    /**
     * 检查用户是否有有效的会员卡
     */
    @GetMapping("/check-valid/{userId}")
    public Result checkValidCard(@PathVariable Long userId) {
        try {
            boolean hasValidCard = memberCardRecordService.hasValidCard(userId);
            return Result.success(hasValidCard, "检查会员卡状态成功");
        } catch (Exception e) {
            log.error("检查会员卡状态失败 - 用户ID: {}, 错误: {}", userId, e.getMessage());
            return Result.error("检查会员卡状态失败");
        }
    }

    /**
     * 获取用户当前有效的会员卡
     */
    @GetMapping("/current/{userId}")
    public Result getCurrentValidCard(@PathVariable Long userId) {
        try {
            MemberCardRecord currentCard = memberCardRecordService.getCurrentValidCard(userId);
            if (currentCard != null) {
                return Result.success(currentCard, "获取当前会员卡成功");
            } else {
                return Result.success(null, "用户暂无有效会员卡");
            }
        } catch (Exception e) {
            log.error("获取当前会员卡失败 - 用户ID: {}, 错误: {}", userId, e.getMessage());
            return Result.error("获取当前会员卡失败");
        }
    }
    
    /**
     * 获取用户所有有效的会员卡
     */
    @GetMapping("/valid/{userId}")
    public Result getAllValidCards(@PathVariable Long userId) {
        try {
            List<MemberCardRecord> validCards = memberCardRecordService.getAllValidCards(userId);
            return Result.success(validCards, "获取所有有效会员卡成功");
        } catch (Exception e) {
            log.error("获取所有有效会员卡失败 - 用户ID: {}, 错误: {}", userId, e.getMessage());
            return Result.error("获取所有有效会员卡失败");
        }
    }

    /**
     * 根据ID获取会员卡记录详情
     */
    @GetMapping("/{recordId}")
    public Result getRecordById(@PathVariable Long recordId) {
        try {
            MemberCardRecord record = memberCardRecordService.getById(recordId);
            if (record != null) {
                return Result.success(record, "获取会员卡记录详情成功");
            } else {
                return Result.error("会员卡记录不存在");
            }
        } catch (Exception e) {
            log.error("获取会员卡记录详情失败 - 记录ID: {}, 错误: {}", recordId, e.getMessage());
            return Result.error("获取会员卡记录详情失败");
        }
    }
}